var BaseUserFeedBack = app.controller('BaseUserFeedBack', function($rootScope, $scope, $http,$state,$interval,$cordovaFileTransfer,BaseUserFeedBackData) {
	try{
		$rootScope.showf=false;
		$scope.show=true;
		$scope.playm='tutor_video.png';
		$scope.voice_path="";
		$scope.content="";
		$scope.voice_size = 0;
		
		var url=SITE_URL+"index.php?app=baseloginapp&callback=JSON_CALLBACK&act=none";
		$http.jsonp(url).success(function(data) {
		});

		$scope.pub=function(){
			var voice_path=$scope.voice_path;
			var voice_size=$scope.voice_size;
			if(voice_path==""||voice_path==undefined||voice_path==null||voice_size<=0){
				if($scope.content==""){
					showtoast('请填写反馈意见');
					return;
				}
			}
			var url=SITE_URL+"index.php?app=baseloginapp&callback=JSON_CALLBACK&act=feedback&c="+$scope.content+"&vp="+$scope.voice_path;
			$http.jsonp(url).success(function(data) {
				showtoast(data.msg);
				$rootScope.back();
			});
		}
		
		$scope.vshowclick=function(){
			$scope.show=!$scope.show;
			if($rootScope.isf){
				$scope.audiostart();
				setTimeout(function () {
					$scope.audiostop();
				}, 500);
			}

		}

		$scope.itemOnLongPress = function() {
			$scope.audiostart();
		}

		$scope.itemOnTouchEnd = function() {
			$scope.audiostop();
		}

		$scope.itemOnTouchMove = function() {
			$scope.audioclear();
		}

		$scope.timer=null;
		$scope.v_start=false;
		$scope.ms=0;
		$scope.ss=0;
		$scope.v_move = 0;
		$scope.audiostart=function(){

			$scope.voice_size = 0;
			$scope.ms=0;
			$scope.ss=0;
			$scope.voice_path="";
			$scope.v_start=true;
			$scope.v_move = 0;
			if($scope.timer!=null)
				$interval.cancel($scope.timer);
			$scope.timer =  $interval(function(){			
				if($scope.v_start){
					$scope.voice_size++;
					$scope.ms=parseInt($scope.voice_size/60);
					$scope.ss=parseInt($scope.voice_size%60);
				}else{
					$interval.cancel($scope.timer);
				}
			},1000);
			console.log('start');
			window.plugins.audioRecorderAPI.record(function(msg) {
				// complete
				console.log('record ok: ' + msg);
			}, function(msg) {
				// failed
				console.log('record err'+msg);
			}, 600); // record 30 seconds
		};

		$scope.audioclear=function(){
			$scope.v_start=false;
			$scope.voice_path="";
			$scope.voice_size=0;
			$scope.v_move = 0;
			$scope.ms=0;
			$scope.ss=0;
			if($scope.timer!=null)
				$interval.cancel($scope.timer);
			console.log('clear');
			if ($rootScope.media != undefined && $rootScope.media != null) {
				$rootScope.media.release();
			}
		};
		$scope.audiostop=function(){
			$scope.v_start=false;
			$scope.v_move = 0;
			if($scope.timer!=null){
				$interval.cancel($scope.timer);
			}
			console.log('end');
			window.plugins.audioRecorderAPI.stop(function(audiopath) {
				// success
				console.log('ok: ' + audiopath);
				if($rootScope.isf){
					$rootScope.isf=false;
					return false;
				}
				if($scope.voice_size<3){
					showtoast('录音时间太短');
					return false;
				}
				$scope.upfile(audiopath);
				$scope.$apply();
			}, function(msg) {
				// failed
				//alert('ko: ' + msg);
				console.log('stop err'+msg);
			});
		};

		$scope.upfile=function(audiopath){
			// 上传
			msglengthapp=0;
			url=$rootScope.site+"index.php?app=usermyapp&act=upfile&callback=JSON_CALLBACK";
			$http.jsonp(url).success( function(data) {
				console.log(data);
				if(data.done){
					$rootScope.ECM_ID=data.retval.userid;
					$rootScope.HTTP_USER_AGENT = data.retval.HTTP_USER_AGENT;
					var uri = $rootScope.site+'index.php?app=appuploadvideo&ajax=ajax&XDEBUG_SESSION_START=ECLIPSE_DBGP';
					console.log(uri);
					var options = new FileUploadOptions();
					options.fileKey="file";
					options.fileName=audiopath.substr(audiopath.lastIndexOf('/')+1);
					options.mimeType="text/plain";
					var params = {};
					params.ECM_ID = $rootScope.ECM_ID;
					params.HTTP_USER_AGENT = $rootScope.HTTP_USER_AGENT;
					params.belong = 10;
					params.item_id = JSON.parse(window.localStorage.user).CHATUSER;;
					options.params = params;
					ProgressIndicator.showProgressBar('正在发送...');
					$cordovaFileTransfer.upload(uri,audiopath,options).then(function (r) {
						console.log("Code = " + r.responseCode);
						console.log("Response = " + r.response);
						console.log("Sent = " + r.bytesSent);
						ProgressIndicator.hide();
						//alert(r.response);
						data = JSON.parse(r.response);
						if(data.done){
							$scope.voice_path=data.retval.file_path;
						}
						else{
							showtoast(data.msg);
						}
					}, function (error) {
						console.log("An error has occurred: Code = " + error.code);
						console.log("upload error source " + error.source);
						console.log("upload error target " + error.target);
						ProgressIndicator.hide();
						showtoast('发送失败，'+error);
					}, function(progressEvent) {
						ProgressIndicator.setProgress(progressEvent.loaded / progressEvent.total);
					});
				}
			});
		}

		$scope.isplay=false;
		$scope.play = function(){
			try{
				if($scope.voice_path!=null&&$scope.voice_path!=''){
					//播放音频 m 即为音频地址
					if(!$scope.isplay){
						if($rootScope.media!=undefined&&$rootScope.media!=null){
							$rootScope.media.release();
						}
						
						$rootScope.media=audioplay($rootScope.site+$scope.voice_path);
						console.log('newplay');
						console.log($rootScope.media.src);
						$rootScope.media.successCallback=function(){
							$scope.playm="tutor_video.png";
							$scope.isplay=false;
							console.log('successCallback png');
							console.log($scope.playm);
							$scope.$apply();
						};
						$rootScope.media.errorCallback=function(err){
							$scope.playm="tutor_video.png";
							$scope.isplay=false;
							console.log('successCallback png');
							console.log($scope.playm);
							$scope.$apply();
						};

						$scope.playm="write_d.gif";
						console.log('newplay png');
						console.log($scope.playm);
						$rootScope.media.play();
						$scope.isplay=true;
					}
				}
			}
			catch(e){
				console.log('play');
				console.log(e);
			}
		}

	}
	catch (e) {
		showcatch(e);
	}
});
BaseUserFeedBack.BaseUserFeedBackData=function($q, $http,$stateParams,$rootScope){
	try {
		var data = [];
		var def = $q.defer();
		if(window.localStorage.u==undefined||window.localStorage.u==null||window.localStorage.u==""){			
			$rootScope.go("loginmain");
		}else{
			def.resolve(data);
		}
		
		return def.promise;
	} catch (e) {
		showcatch(e);
	}
}